package net.yxsoft.service;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import net.yxsoft.kit.Helper;
import net.yxsoft.model.formdesigner.FnFormData;
import net.yxsoft.resource.CodeRes;
import net.yxsoft.service.common.BaseService;
import rapid.core.TResult;

import java.util.Date;
import java.util.List;

/**
 * @author wj
 * 2020-5-8
 * 链卡
 */
public class ApplyService extends BaseService {


  public static final ApplyService INSTANCE = new ApplyService();






    /**
    * @MethodName: save
    * @Description:
    * @param: sjhm
     * @param: ickh
     * @param: khh
     * @param: yhkh
     * @param: cid
     * @param: authuser
    * @return rapid.core.TResult
    * @throws
    * @date 2023/9/6 17:35
    * @author jhf
    */
    public TResult save(FnFormData fnFormData ,String cid) {
        if(StrKit.notNull(fnFormData)) {
            if(cid==null){


            String cidnew= Helper.bulidUUID();
            Date date = new Date(System.currentTimeMillis());
            fnFormData.setCreated(date);
            fnFormData.setConflictId(cidnew);
            fnFormData.setRowId(cidnew);
            fnFormData.save();
            }else {
                fnFormData.setConflictId(cid);
                fnFormData.setRowId(cid);
                fnFormData.update();
            }

        }

        return success();
    }




    /**
    * @MethodName: getBlockListall
    * @Description: 获取跨链区块链消息列表
    * @param: rqks
     * @param: rqjs
     * @param: hs
     * @param: kg
     * @param: jd
     * @param: val
    * @return java.util.List<com.jfinal.plugin.activerecord.Record>
    * @throws
    * @date 2023/10/16 16:25
    * @author wj
    */
    public List<Record> getBlockListall(String rqks,String rqjs,String hs,String kg,String jd,String val){

        String sql=" ";
        if (StrKit.notBlank(rqks)){
            sql+=" and attrib_26 >= STR_TO_DATE('"+rqks+"', '%Y-%m-%d') ";
        }
        if (StrKit.notBlank(rqjs)){
            sql+=" and attrib_26 <= STR_TO_DATE('"+rqjs+"', '%Y-%m-%d') ";
        }

        if (StrKit.notBlank(hs)){
            sql+=" and attrib_12 ='"+hs+"' ";
        }

        if (StrKit.notBlank(kg)){
            sql+=" and attrib_11 ='"+kg+"' ";
        }

        if (StrKit.notBlank(jd)){
            sql+=" and attrib_10 ='"+jd+"' ";
        }


        List<Record> applys=   Db.find("SELECT  *,count(conflict_id) txcount,DATE_FORMAT(attrib_26, '%Y-%m-%d %H:%i:%s') attrib_26_cn FROM s_ctransaction where attrib_02=? "+sql+" group by attrib_11 order by  attrib_26 desc",val);
        return  applys;
    }


    /**
    * @MethodName: getTxListall
    * @Description: 获得交易列表
    * @param: rqks
     * @param: rqjs
     * @param: hs
     * @param: lid
     * @param: fq
     * @param: val
    * @return java.util.List<com.jfinal.plugin.activerecord.Record>
    * @throws
    * @date 2023/10/23 16:10
    * @author wj
    */
    public List<Record> getTxListall(String rqks,String rqjs,String hs,String lid,String fq,String val){

        String sql=" ";
        if (StrKit.notBlank(rqks)){
            sql+=" and attrib_14 >= STR_TO_DATE('"+rqks+"', '%Y-%m-%d') ";
        }
        if (StrKit.notBlank(rqjs)){
            sql+=" and attrib_14 <= STR_TO_DATE('"+rqjs+"', '%Y-%m-%d') ";
        }

        if (StrKit.notBlank(hs)){
            sql+=" and attrib_13 ='"+hs+"' ";
        }

        if (StrKit.notBlank(lid)){
            sql+=" and attrib_02 ='"+lid+"' ";
        }

        if (StrKit.notBlank(fq)){
            sql+=" and attrib_01 ='"+fq+"' ";
        }


        List<Record> applys=   Db.find("SELECT  *,DATE_FORMAT(attrib_14, '%Y-%m-%d %H:%i:%s') attrib_14_cn  FROM s_ctransaction where attrib_02=? "+sql+"  order by  attrib_14 desc",val);
        return  applys;
    }







    /**
    * @MethodName: getBlockListcount
    * @Description: 获取知道链下的区块信息
    * @param: rqks
     * @param: rqjs
     * @param: hs
     * @param: kg
     * @param: jd
     * @param: val
    * @return com.jfinal.plugin.activerecord.Record
    * @throws
    * @date 2023/10/16 16:18
    * @author wj
    */
    public Record getBlockListcount(String rqks,String rqjs,String hs,String kg,String jd,String val){

        String sql=" ";
        if (StrKit.notBlank(rqks)){
            sql+=" and attrib_26 >= STR_TO_DATE('"+rqks+"', '%Y-%m-%d') ";
        }
        if (StrKit.notBlank(rqjs)){
            sql+=" and attrib_26 <= STR_TO_DATE('"+rqjs+"', '%Y-%m-%d') ";
        }

        if (StrKit.notBlank(hs)){
            sql+=" and attrib_12 ='"+hs+"' ";
        }

        if (StrKit.notBlank(kg)){
            sql+=" and attrib_11 ='"+kg+"' ";
        }

        if (StrKit.notBlank(jd)){
            sql+=" and attrib_10 ='"+jd+"' ";
        }



        Record apply=   Db.findFirst("SELECT count(*) as count FROM s_ctransaction where attrib_02=? "+sql+" group by attrib_11 ",val);
        return  apply;
    }



    /**
     * @MethodName: getBlockidByTxList
     * @Description: 获取区块下交易列表
     * @param: cid
     * @return java.util.List<com.jfinal.plugin.activerecord.Record>
     * @throws
     * @date 2023/10/16 21:18
     * @author wj
     */
    public List<Record> getBlockidByTxList(int blocknumber,String chainid){

        return Db.find("select * from s_ctransaction where attrib_11=? and attrib_02=?",blocknumber,chainid);
    }


    /**
    * @MethodName: getTxinfo
    * @Description: 获取交易详情
    * @param: cid
    * @return com.jfinal.plugin.activerecord.Record
    * @throws
     * @date 2023/10/25 11:21
     * @author wj
    */
    public Record getTxinfo(String cid){

        return Db.findFirst("select *,DATE_FORMAT(attrib_14, '%Y-%m-%d %H:%i:%s') attrib_14_cn from s_ctransaction where conflict_id=? ",cid);
    }


    /**
     * @MethodName: getBlockInfo
     * @Description: 获取区块详情
     * @param: cid
     * @return com.jfinal.plugin.activerecord.Record
     * @throws
     * @date 2023/10/16 21:24
     * @author wj
     */
    public Record getBlockInfo(String cid){
        return Db.findFirst("select *,DATE_FORMAT(attrib_26, '%Y-%m-%d %H:%i:%s') attrib_26_cn from  s_ctransaction where conflict_id=?",cid);
    }

}
